/**
	The Minerva Project, DAG Scheduling Tools
	Copyright (C) 2006 Florin Pop & Stefan Bucur

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 */
package ro.pub.cs.minerva.task;

import java.io.File;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;

import ro.pub.cs.minerva.utils.Constants;

/**
 * @author Florin Pop
 *
 */
public class DepsConfiguration {

	private String depsFile;
	private TasksConfiguration tc;
	private TaskDepsGraph dag;

	/**
	 * @param depsFile
	 * @param dependencyList
	 */
	public DepsConfiguration(String depsFile, String taskTile) {
		this.depsFile = depsFile;
		this.dag = new TaskDepsGraph();
		tc = new TasksConfiguration(taskTile);
		this.getDepsFromFile();
	}
	
	/**
	 * @param depsFile
	 * @param dependencyList
	 */
	public DepsConfiguration(String depsFile, TaskDepsGraph dag) {
		this.depsFile = depsFile;
		this.dag = dag;
	}
	
	private void getDepsFromFile() {
		
		try {
			Scanner scanner = new Scanner(new File(depsFile));
			scanner.useDelimiter(System.getProperty("line.separator")); 
			while (scanner.hasNext()) {
				this.parseLine(scanner.next());
			}
			scanner.close();
		} catch (Exception e) {
			System.err.println("File parsing error: "+e.getMessage());
		}
	}
	
	private void parseLine(String line) {
		try {
			Scanner lineScanner = new Scanner(line);
			lineScanner.useDelimiter(" ");
			if(lineScanner.hasNext(Constants.COMMENT))
				return;
			
			Vector<String> parents = new Vector<String>();
	        Vector<String> childs = new Vector<String>();
			
			if(lineScanner.hasNext("PARENT"))
				lineScanner.next();
			
			while(!lineScanner.hasNext("CHILD")) {
				parents.add(lineScanner.next());
			}
			
			if(lineScanner.hasNext("CHILD"))
				lineScanner.next();
			
			while(!lineScanner.hasNext("COST")) {
				childs.add(lineScanner.next());
			}
			
			if(lineScanner.hasNext("COST"))
				lineScanner.next();
			
			int cost = lineScanner.nextInt();
			
			Iterator<String> pit = parents.iterator();
	        while(pit.hasNext()) {
	        	Task parent = tc.getTask(pit.next());
	        	
	        	if(!dag.containsNode(parent)) dag.addNode(parent);
	        	
	        	Iterator<String> cit = childs.iterator();
	        	while(cit.hasNext()) {
	        		Task child = tc.getTask(cit.next());
	        		
	        		if(!dag.containsNode(child)) dag.addNode(child);
	        		
	        		Dependency d = new Dependency(parent,child);
	        		d.setDelayTime(cost);
	        		
	        		if(!dag.containsLink(d)) dag.addLink(d);
	        	}
	        }
		} catch (Exception e) {
			System.err.println("Line parsing error: "+e.getMessage());
		}
	}

	/**
	 * @return the dependencyList
	 */
	public TaskDepsGraph getDAG() {
		return dag;
	}

	/**
	 * @param dependencyList the dependencyList to set
	 */
	public void setDAG(TaskDepsGraph dag) {
		this.dag = dag;
	}

	/**
	 * @return the depsFile
	 */
	public String getDepsFile() {
		return depsFile;
	}

	/**
	 * @param depsFile the depsFile to set
	 */
	public void setDepsFile(String depsFile) {
		this.depsFile = depsFile;
	}
}
